package com.gs.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.gs.pojo.StayRegister;
import com.gs.pojo.User;
import com.gs.service.stayregister.StayRegisterService;
import com.gs.service.user.UserService;
import com.gs.utils.Constants;

@Controller
@RequestMapping("/user")
public class LoginController {

	@Resource
	private UserService userService;

	@Resource
	private StayRegisterService stayRegisterService;

	@RequestMapping("/login.html")
	public String tologin(){
		return "login/login";
	}

	@RequestMapping("/todata")
	public String todate(){
		return "redirect:/DateView/tolist.do";
	}

	@RequestMapping("/login")
	public String login (@RequestParam("username")String userName, 
			@RequestParam("password")String password,Model model,HttpSession session) {
		User user = userService.login(userName, password);
		double totalMoneyOne = 0;
		double totalMoneyTwo = 0;
		List<StayRegister> list = stayRegisterService.selectAll();
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getReceiveTargetID() == 2) {
				totalMoneyOne+=list.get(i).getSumConst();
			} else {
				totalMoneyTwo+=list.get(i).getSumConst();
			}
		}
		if (user != null) {
			session.setAttribute(Constants.USER_SESSION, user);
			model.addAttribute("totalMoneyOne", totalMoneyOne);
			model.addAttribute("totalMoneyTwo", totalMoneyTwo);
			return "main/main";
		}
		return "redirect:login.html";
	}


	//注销
	@RequestMapping(value="/logout")
	public String logout(HttpSession session){
		session.removeAttribute(Constants.USER_SESSION);
		return "redirect:login.html";
	}
}
